# Contributor: Konstantin Kulikov <k.kulikov2@gmail.com>
# Maintainer: Konstantin Kulikov <k.kulikov2@gmail.com>
pkgname=victoria-metrics
pkgver=1.40.0
pkgrel=0
provides=victoriametrics
pkgdesc="Fast, cost-effective and scalable time series database"
url="https://github.com/VictoriaMetrics/VictoriaMetrics"
# Test failures on other archs, FP precision mostly.
arch="x86_64"
license="Apache-2.0"
makedepends="go"
subpackages="$pkgname-openrc $pkgname-tools:_tools $pkgname-backup-tools:_backup_tools"
install="$pkgname.pre-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/VictoriaMetrics/VictoriaMetrics/archive/v$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	"
builddir="$srcdir/VictoriaMetrics-$pkgver"

# Depends on cgo zstd package that ships prebuilt object files built against glibc.
# It is possible to rebuild it against musl, but
# pure go zstd lib works well enough and is not written in C.
export CGO_ENABLED=0

export GOPATH=${GOPATH:-$srcdir/go}
export GOCACHE=${GOCACHE:-$srcdir/go-build}
export GOTMPDIR=${GOTMPDIR:-$srcdir}

build() {
	local ldflags="-w -s -X github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo.Version=$pkgver"
	local bin
	for bin in victoria-metrics vmagent vmalert vmauth vmrestore vmbackup; do
		go build -mod=vendor -ldflags="$ldflags" -o bin/$bin ./app/$bin
	done
}

check() {
	go test -short -mod=vendor ./...
}

package() {
	install -Dm755 $builddir/bin/$pkgname $pkgdir/usr/bin/$pkgname
	install -Dm755 $srcdir/$pkgname.initd $pkgdir/etc/init.d/$pkgname
	install -Dm644 $srcdir/$pkgname.confd $pkgdir/etc/conf.d/$pkgname
}

_tools() {
	pkgdesc="$pkgdesc (vmagent, vmalert, vmauth)"
	install -Dm755 $builddir/bin/vmagent $subpkgdir/usr/bin/vmagent
	install -Dm755 $builddir/bin/vmalert $subpkgdir/usr/bin/vmalert
	install -Dm755 $builddir/bin/vmauth $subpkgdir/usr/bin/vmauth
}

_backup_tools() {
	pkgdesc="$pkgdesc (vmbackup, vmrestore)"
	install -Dm755 $builddir/bin/vmbackup $subpkgdir/usr/bin/vmbackup
	install -Dm755 $builddir/bin/vmrestore $subpkgdir/usr/bin/vmrestore
}

sha512sums="0602d4951a914a2caa0bb81374b20b8f229a543aa520a617f5a847db19a706befd0fa2f654cca4f9f6a81c1f53ea5641b4627cf32d284eb161f778add7171ede  victoria-metrics-1.40.0.tar.gz
6e6fee6240abf9aab9a6c6174bf44394cd09c6e0c5b2743bef520ee6660f48f209e8f2f12c79b4d8546f2d1468d90409dac9c2b693218bede516a0f715c64c48  victoria-metrics.initd
0b5150faf35695f61255e957757c560bf92f0c75317a61a900a6a179e18d918ed5bd7d1405053f10fd93c64ae0e873ab0855044abcab9883bbe3d32ba1079041  victoria-metrics.confd"
